package com.renne.email.service.impl;

import java.util.List;

import com.renne.common.exception.ServiceException;
import com.renne.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.renne.common.utils.DateUtils;
import com.renne.common.utils.SecurityUtils;
import com.renne.common.core.domain.entity.SysUser;
import com.renne.common.core.domain.model.LoginUser;
import com.renne.common.utils.uuid.IdUtils;
import com.renne.common.utils.bean.BeanUtils;
import com.renne.common.core.text.Convert;
import com.renne.email.domain.MailSenderConfigQuery;
import com.renne.email.domain.MailSenderConfigVo;
import com.renne.email.domain.MailSenderConfig;
import com.renne.email.mapper.MailSenderConfigMapper;
import com.renne.email.service.IMailSenderConfigService;


/**
 * 发件人邮箱配置Service业务层处理
 *
 * @author LiuYu
 * @date 2024-07-26
 */
@Service
public class MailSenderConfigServiceImpl implements IMailSenderConfigService {
    @Autowired
    private MailSenderConfigMapper mailSenderConfigMapper;

    /**
     * 查询启用的发件人邮箱配置
     *
     * @return 发件人邮箱配置
     */
    @Override
    public MailSenderConfigVo getEnabledMailSenderConfigVo() {

        // 获取邮件发送配置信息
        MailSenderConfigQuery query = new MailSenderConfigQuery();
        query.setEnable("1");
        List<MailSenderConfigVo> mailSenderConfigVos = mailSenderConfigMapper.selectMailSenderConfigVoList(query);
        if (StringUtils.isNull(mailSenderConfigVos) || mailSenderConfigVos.isEmpty()) {
            return null;
        } else if (mailSenderConfigVos.size() > 1) {
            throw new ServiceException("请先配置一个邮件发送配置信息！");
        }

        return mailSenderConfigVos.get(0);
    }

    /**
     * 查询发件人邮箱配置
     *
     * @param senderEmailConfigId 发件人邮箱配置ID
     * @return 发件人邮箱配置
     */
    @Override
    public MailSenderConfigVo getMailSenderConfigVoById(String senderEmailConfigId) {
        return mailSenderConfigMapper.selectMailSenderConfigVoById(senderEmailConfigId);
    }

    /**
     * 查询发件人邮箱配置列表
     *
     * @param query 发件人邮箱配置
     * @return 发件人邮箱配置集合
     */
    @Override
    public List<MailSenderConfigVo> getMailSenderConfigVoList(MailSenderConfigQuery query) {
        return mailSenderConfigMapper.selectMailSenderConfigVoList(query);
    }

    /**
     * 新增发件人邮箱配置
     *
     * @param mailSenderConfigVo 发件人邮箱配置Vo
     * @return 结果
     */
    @Override
    public int saveMailSenderConfigVo(MailSenderConfigVo mailSenderConfigVo) {
        MailSenderConfig mailSenderConfig = new MailSenderConfig();
        BeanUtils.copyBeanProp(mailSenderConfig, mailSenderConfigVo);
        mailSenderConfig.setMailSenderConfigId(IdUtils.fastSimpleUUID());
        LoginUser loginUser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginUser.getUser();
        mailSenderConfig.setDeptId(sysUser.getDeptId());
        mailSenderConfig.setCreateById(SecurityUtils.getUserId());
        mailSenderConfig.setCreateBy(SecurityUtils.getUsername());
        mailSenderConfig.setCreateTime(DateUtils.getNowDate());
        if (!checkMailSenderConfigUnique() && "1".equals(mailSenderConfig.getEnable())) {
            throw new ServiceException("已存在启用的邮箱配置，当前邮箱不能保存为启用状态！");
        }
        return mailSenderConfigMapper.insertMailSenderConfig(mailSenderConfig);

    }

    /**
     * 修改发件人邮箱配置
     *
     * @param mailSenderConfigVo 发件人邮箱配置Vo
     * @return 结果
     */
    @Override
    public int updateMailSenderConfigVo(MailSenderConfigVo mailSenderConfigVo) {
        MailSenderConfig mailSenderConfig = new MailSenderConfig();
        BeanUtils.copyBeanProp(mailSenderConfig, mailSenderConfigVo);
        mailSenderConfig.setUpdateBy(SecurityUtils.getUsername());
        mailSenderConfig.setUpdateTime(DateUtils.getNowDate());
        MailSenderConfigVo enabledMailSenderConfigVo = getEnabledMailSenderConfigVo();
        if (null != enabledMailSenderConfigVo && !mailSenderConfig.getMailSenderConfigId().equals(enabledMailSenderConfigVo.getMailSenderConfigId())) {
            throw new ServiceException("已存在启用的邮箱配置，当前邮箱不能修改为启用状态！");
        }
        return mailSenderConfigMapper.updateMailSenderConfig(mailSenderConfig);
    }

    /**
     * 删除发件人邮箱配置对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteMailSenderConfigByIds(String ids) {
        return mailSenderConfigMapper.deleteMailSenderConfigByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除发件人邮箱配置信息
     *
     * @param senderEmailConfigId 发件人邮箱配置ID
     * @return 结果
     */
    @Override
    public int deleteMailSenderConfigById(String senderEmailConfigId) {
        return mailSenderConfigMapper.deleteMailSenderConfigById(senderEmailConfigId);
    }

    /**
     * 校验发件人邮箱配置名称是否唯一
     *
     * @param senderEmailConfigName 发件人邮箱配置名称
     * @return 结果
     */
    @Override
    public Boolean checkMailSenderConfigNameUnique(String senderEmailConfigName) {
        if (StringUtils.isNull(senderEmailConfigName)) {
            return Boolean.FALSE;
        } else {
            MailSenderConfigQuery query = new MailSenderConfigQuery();
            query.setEmailEq(senderEmailConfigName);
            List<MailSenderConfigVo> mailSenderConfigVos = mailSenderConfigMapper.selectMailSenderConfigVoList(query);
            if (StringUtils.isNull(mailSenderConfigVos) || mailSenderConfigVos.isEmpty()) {
                return Boolean.TRUE;
            } else {
                return Boolean.FALSE;
            }
        }
    }

    /**
     * 校验发件人邮箱配置是否唯一
     *
     * @return 结果
     */
    @Override
    public Boolean checkMailSenderConfigUnique() {
        MailSenderConfigQuery query = new MailSenderConfigQuery();
        query.setEnable("1");
        List<MailSenderConfigVo> mailSenderConfigVos = mailSenderConfigMapper.selectMailSenderConfigVoList(query);
        if (StringUtils.isNull(mailSenderConfigVos) || mailSenderConfigVos.isEmpty()) {
            return Boolean.TRUE;
        } else {
            return Boolean.FALSE;
        }
    }
}
